<?php
	$cookie = dirname(__FILE__) . DIRECTORY_SEPARATOR . "tmp/cookies.txt";
	 
	 
	//文件不存在则新建一个
	if(!file_exists($cookie)) {
		$handle = fopen($cookie , "w");
		fclose($handle);
	}	
	
	$ua = "Mozilla/5.0 (Windows NT 5.1; rv:6.0.1) Gecko/20100101 Firefox/6.0.1 ";
	
	
if($_SERVER['REQUEST_METHOD']=='GET') {
	


	
	
	//网页
	$page_url = 'http://yjcx.chinapost.com.cn/index.jsp';
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $page_url);
	//curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
	curl_setopt($ch, CURLOPT_HEADER, 0);	
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, $ua);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION ,1);
	$page = curl_exec($ch);
	curl_close($ch);	
	/*
	<input type="hidden" name="action" value="getBatchMailTrace"/>
<input type="hidden" name="isin" value="1"/>
<input type="hidden" name="jsessionid" value="Gn1vTjhGJV6Nrvj6pNyGQF4y27rs39JkH3ktClk3BDSQdnTn8hvK!120648370!1319330244655"/>
	*/
	$regx = '#<input type="hidden" name="(.*?)" value="(.*?)"/>#';
	preg_match_all($regx,$page,$m);
	//var_dump($m[1]);
	echo $jsessionid = $m[2][2];
	//die($jsessionid);
	
	
	/*
	$img_url = "http://yjcx.chinapost.com.cn/image";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $img_url);
	curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
	curl_setopt($ch, CURLOPT_HEADER, 0);	
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, $ua);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION ,1);
	$img = curl_exec($ch);
	file_put_contents('img.jpg',$img);
	*/
	
}else {
	//var_dump($_POST);
	unset($_POST['Submit']);
	$params = $_POST;
	var_dump($params);
	//die();
	$post_url = 'http://yjcx.chinapost.com.cn/queryMail.do';
	$refer = 'http://yjcx.chinapost.com.cn/';
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $post_url);
	curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
	//curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
	curl_setopt($ch, CURLOPT_HEADER, 0);	
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, $ua);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION ,1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	curl_setopt($ch, CURLOPT_REFERER, $refer);
	
	$post_page = curl_exec($ch);
	
	
	curl_close($ch);	
	var_dump($post_page);
	die();
	$regx = '#<td class="txt-main" align="left">(.*)<br /></td>#iUs';
	preg_match($regx,$post_page,$m);
	print($m[1]);
	
	die();
	
}
	
?>
<html>
<body>
			<table>
			<form method="post" action="">
				<tbody><tr>
					<td class="tdd" height="100">
						单号：					</td>

	  <td>
	  <input type="hidden" name="action" value="getBatchMailTrace"/>
<input type="hidden" name="isin" value="1"/>
<input type="hidden" name="jsessionid" value="<?php echo $jsessionid;?>"/>
						<input name="mailCode" value="PA27665500931" type="text">						
				  </td>

					<td>
					<span class="tdd">请输入验证码：</span>
					<input name="code" value="" size="6" maxlength="6" autocomplete="off" type="text">			
					<img src="img.php" alt="如验证码没有显示，请过10分钟再试" name="validatecodeimg" align="absmiddle" border="0">					</td>

<!--
<input type="hidden" name="optijiaot.x" value="22"/>
<input type="hidden" name="optijiaot.y" value="14"/>
-->

<td>

						<input name="Submit" value="EMS单号查询" type="submit"> 
					</td>
			  </tr>
	  </tbody>
	  </form></table>
</body>
</html>
	
	
	